package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
//import java.io.FileNotFoundException;
//import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class findCommonFriends {
	
	public static Map<String,List<String>> getListFromString(Map<String,List<String>> results , String scr){
//		Map<String,List<String>> results = new HashMap<String,List<String>>();
		List<String> temp_ls = new ArrayList<String>();
		String[] temp = scr.split("\t");
		if(temp.length < 2){
			return null;
		}
		for(int i=1; i<temp.length;i++){
			temp_ls.add(temp[i]);
		}
		if(!results.containsKey(temp[0])){
			results.put(temp[0], temp_ls);
		}
		return results;
	}
	
	public static String StrNum(int num){
		StringBuffer results = new StringBuffer(String.valueOf(num));
		int nums = results.length();
		for(int i=0;i<(5-nums);i++){
			results.insert(0, "0");
		}
		return results.toString();
	}
	
	public static Map<String,List<String>> getMapFromFile(String[] args) throws IOException{
		Map<String,List<String>> results = new HashMap<String,List<String>>();
		Integer num = new Integer(0);
		String filePath = "/home/iir/findfriends/part-r-" ;
//		int min = Integer.valueOf(args[1]);
//		int max = Integer.valueOf(args[2]);		
		for(int i = 0;i<30;i++){
			String fileName = filePath + StrNum(i);
			System.out.println(fileName);
			BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
			while(true){
				String temps = new String("");
				temps = br.readLine();
				if(null == temps){
					break;
				}
				results = getListFromString(results,temps);
				System.out.println(num++);
			}
		}
		
		return results;
	}
	
	public static List<String> intersectList(List<String> ls_a, List<String> ls_b){
		List<String> results = new ArrayList<String>();
		Map<String,Boolean> temp = new HashMap<String,Boolean>();
		for(String a : ls_a){
			if(!temp.containsKey(a)){
				temp.put(a, false);
			}
		}
		for(String b : ls_b){
			if(temp.containsKey(b)){
				temp.remove(b);
				temp.put(b, true);
			}
		}
		Iterator<String> it_temp = temp.keySet().iterator();
		while(it_temp.hasNext()){
			String tes = new String(it_temp.next());
			if(temp.get(tes)){
				results.add(tes);
			}
		}
		return results;		
	}
	
	public static void main(String[] args) throws IOException{
//		String filePath = "/home/mblank/wise/dataset/fsn/findfriends/000";
//		if(args.length < 3){
//			System.out.println("input more parameters!");
//			return ;
//		}
		
		Map<String,List<String>> results = new HashMap<String,List<String>>();
		results = getMapFromFile(args);
		
		String filePath_sn = "/home/iir/sn/000";
		Integer num = new Integer(0);
		BufferedReader br = new BufferedReader(new FileReader(new File(filePath_sn)));
		while(true){
			String temps = new String("");
			List<String> temp_ls = new ArrayList<String>();
			temps = br.readLine();
			if(null == temps){
				break;
			}
//			results = getListFromString(results,temps);
			String[] uids = temps.split("\t");
			if(uids.length != 2){
				continue;
			}
			temp_ls = intersectList(results.get(uids[0]) , results.get(uids[1]));
			if(temp_ls == null)
				continue;
			FileWriter fw = new FileWriter(filePath_sn + "_out",true);   
				fw.write(temps+"/n");
				fw.flush(); 
				fw.close();
			System.out.println(num++);
		}
		
		
		
		
		
		
	}

}
